using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Outlook;
using System.Net;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;


namespace TXLooker.Common.Outlook
{
    public class OutlookGlobal
    {
        public static void RemoveFolderByName(MAPIFolder parent, string foldername )
        {
            int index = GetFolderIndex( parent, foldername );
            if( index != -1 )
            {
                try
                {
                    parent.Folders.Remove( index );
                }
                catch (System.Exception ex)
                {
                    Trace.WriteLine( ex.ToString() );
                }
            }
        }
        public static void RemoveFolderByID(MAPIFolder parent, string entryID )
        {
            int index = GetFolderIndexByID( parent, entryID );
            if( index != -1 )
            {
                try
                {
                    parent.Folders.Remove( index );
                }
                catch (System.Exception ex)
                {
                    LogManager.WriteMessageToCustomLog("Error in RemoveFolderByID(OutlookGlobal.cs) " + ex.ToString());
                    Trace.WriteLine( ex.ToString() );
                }
            }
        }
        public static int GetFolderIndex( MAPIFolder parent, string foldername )
        {
            for( int index = 1; index <= parent.Folders.Count; index++ )
            {
                MAPIFolder folder = parent.Folders[index];

                if( folder.Name != foldername )
                    continue;

                return index;
            }

            return -1;
        }
        public static int GetFolderIndexByID( MAPIFolder parent, string entryID )
        {
            for( int index = 1; index <= parent.Folders.Count; index++ )
            {
                MAPIFolder folder = parent.Folders[index];

                if( folder.EntryID != entryID )
                    continue;

                return index;
            }

            return -1;
        }
        public static MAPIFolder GetChildFolderByName( MAPIFolder parent, string foldername )
        {
            return FindFolderByName( parent.Folders, foldername );
        }
        public static MAPIFolder GetChildFolderByID( MAPIFolder parent, string entryID )
        {
            return FindFolderByID( parent.Folders, entryID );
        }
        public static MAPIFolder FindFolderByName(Folders folders, string foldername)
        { 
            for( int index = 1; index <= folders.Count; index++ )
            {
                MAPIFolder folder = folders[index];

                if( folder.Name != foldername )
                    continue;

                return folder;
            }

            return null;
        }
        public static MAPIFolder FindFolderByID(Folders folders, string entryID)
        { 
            for( int index = 1; index <= folders.Count; index++ )
            {
                MAPIFolder folder = folders[index];

                if( folder.EntryID != entryID )
                    continue;

                return folder;
            }

            return null;
        }
        public static void ClearFolder( MAPIFolder folder )
        {
            int count = folder.Folders.Count;

            for( int i = count; i > 0; i-- )
            {
                try   { folder.Folders.Remove( i ); }
                catch {  }
            }
        }

        public static string GetStorePath( string strStoreID )
        {
            string tempGetStorePath = null;
            int intStart = 0;
            int intEnd = 0;
            string strProvider = null;
            string strPathRaw = null;
            intStart = ( strStoreID.IndexOf( "0000", 8 ) + 1 ) + 4;
            intEnd = ( strStoreID.IndexOf( "00", intStart - 1 ) + 1 );
            strProvider = strStoreID.Substring( intStart - 1, intEnd - intStart );
            strProvider = Hex2ToString( strProvider );
            switch( strProvider.ToLower() )
            {
                case "mspst.dll":
                case "pstprx.dll":
                    intStart = ( strStoreID.LastIndexOf( "00000000" ) + 1 ) + 8;
                    strPathRaw = strStoreID.Substring( intStart - 1 );
                    tempGetStorePath = Hex4ToString( strPathRaw ).Trim( ' ' );
                    break;
                case "msncon.dll":
                    intStart = ( strStoreID.LastIndexOf( "00", strStoreID.Length - 2 - 1 ) + 1 ) + 2;
                    strPathRaw = strStoreID.Substring( intStart - 1 );
                    tempGetStorePath = Hex2ToString( strPathRaw ).Trim( ' ' );
                    break;
                case "emsmdb.dll":
                    tempGetStorePath = "Exchange store";
                    break;
                default:
                    tempGetStorePath = "Unknown store path";
                    break;
            }

            tempGetStorePath = tempGetStorePath.TrimEnd( new char[] { '\0' } );
            return tempGetStorePath;
        }
        private static string Hex4ToString( string data )
        {
            string strTemp = null;
            string strAll = null;
            int i = 0;
            for( i = 1; i <= data.Length; i += 4 )
            {
                strTemp = data.Substring( i - 1, 4 );
                strTemp = strTemp.Substring( strTemp.Length - 2 ) + strTemp.Substring( 0, 2 );

                long dv = Int64.Parse( strTemp, System.Globalization.NumberStyles.AllowHexSpecifier );
                strAll = strAll + System.Convert.ToChar(dv);
            }
            return strAll;
        }
        private static string Hex2ToString( string data )
        {
            string strTemp = null;
            string strAll = null;
            for( int i = 1; i <= data.Length; i += 2 )
            {
                strTemp = data.Substring( i - 1, 2 );
                long dv = Int64.Parse( strTemp, System.Globalization.NumberStyles.AllowHexSpecifier );
                strAll = strAll + System.Convert.ToChar( dv );
            }
            return strAll;
        }

        public static string CreateFolderWebViewURL(string htmlContent)
        {
            if( string.IsNullOrEmpty( htmlContent ) )
                throw new ArgumentException();

            string url = Path.GetTempFileName();

            using( StreamWriter writer = new StreamWriter(url) )
            {
                writer.Write(htmlContent);
            }

            return url;
        }


        public static void Ol_BeforeFolderMove(MAPIFolder MoveTo, ref bool Cancel)
        {
            Cancel = true;
        }

        public static List<string> GetOlCategories(Application app)
        {
            List<string> listCategories = new List<string>();
            Microsoft.Office.Interop.Outlook.Categories categories = app.Session.Categories;
            for (int i = 1; i <= categories.Count; i++)
            {
                listCategories.Add(categories[i].Name);
            }

            return listCategories;
   
        }

        public static string GetDefaultCategory(Application app)
        {
            List<string> listCategories = GetOlCategories(app);
            return listCategories[0];
        }

        public static Dictionary<string, string> GetOlCategoriesColor(Application app)
        {
            Dictionary<string,string> listCategories = new Dictionary<string,string>();
            Microsoft.Office.Interop.Outlook.Categories categories = app.Session.Categories;
            for (int i = 1; i <= categories.Count; i++)
            {
                listCategories.Add(categories[i].Color.ToString(),categories[i].Name);
            }
            return listCategories;
        }
    }
}
